<?xml version="1.0" encoding="UTF-8"?>
<!-- updated on 6th June
    This file use a fake presentation file called testing.xml under TestingDiagram folder for testing-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xmi="http://schema.omg.org/spec/XMI/2.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:MASC_Profile="http:///schemas/MASC_Profile/_OnYS4BTgEd6yO-ZURVEtWw/6"
    xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
    xmlns:uml="http://www.eclipse.org/uml2/2.1.0/UML"
    xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
    xmlns:masc-gp="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-gp"
    xmlns:masc-se="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-se"
    xmlns:masc-ex="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ex"
    xmlns:masc-om="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-om"
    xmlns:masc-sc="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-sc"
    xmlns:masc-ap="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ap"
    xmlns:masc-mp="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-mp"
    xmlns:masc-up="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-up"
    xmlns:masc-config="http://www.nicta.com.au/projects/armature/masc-config"
    xmlns:wsa="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/wsa"
    xmlns:editmodel="editmodel.xmi"
    xmlns:options="options.xmi"
    xmlns:QoSOntology="http://www.myontologyrepository.com/qosontology.owl"
    xmlns:tns="${currentFileURL}" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs" version="2.0">
    
    <xsl:param name="xmiFile" select="document('../TestingDiagram/UML.xml')"/>
    <xsl:param name="presentationLocation" select="$xmiFile/uml:Model/packagedElement/eAnnotations/@source"/>
    <!-- the two params above only needed while it needs read presentation xml url from XMI -->
    <!-- we assume that presentation xml file name and location already knows -->
    <xsl:param name="presentationFile" select="document('../TestingDiagram/testing.xml')"/>
    <xsl:param name="eventData" select="document('../Fake_MASC_EventData.xml')"/>
    <xsl:param name="measurementData" select="document('../Fake_MASC_MeasurementData.xml')"/>
    <xsl:param name="input" select="document('../Fake_StockTradingGPA.xml')"/>
    
    <xsl:param name="newline" select="'&#10;'"/>
    
    <xsl:template match="/">
        <!--  <xsl:apply-templates select="*"/> -->
        <xsl:value-of select="$newline"/>
        <xsl:call-template name="noteUpdate"/>
    </xsl:template>
    
    <!-- copy all content which @note != 'NOTE'
        for element that @note = 'NOTE', update it -->
    <xsl:template  match="*">
        <xsl:copy>
            <xsl:copy-of  select="@*[not(starts-with(., 'Measurement Runtime Data'))]"/>
            <xsl:apply-templates  select="@note[starts-with(., 'Measurement Runtime Data')]"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    
    <!-- update element with @note = 'NOTE' -->
    <xsl:template  match="@note[starts-with(., 'Measurement Runtime Data')]">
        <xsl:attribute  name="note">
            <xsl:value-of select="."/>
            <xsl:value-of select="$newline"/>
            <xsl:call-template name="noteUpdate"/>
        </xsl:attribute>
    </xsl:template> 
    
    <!-- template for content updated in note -->
    <xsl:template name="noteUpdate">
        <xsl:for-each select="$input/xmi:XMI/MASC_Profile:*">
            <!-- get the MASCID from input XMI's stereotype -->
            <xsl:variable name="MASCID"
            select="@AP-MASCID|@GP-MASCID|@MD-MASCID|@MP-MASCID|@UP-MASCID"/>
            <xsl:variable name="baseElement" select="@base_Element"/>
            <!-- find the corresponding latest runtime data value in MeasurementData file (by matching MASCID with MSD-MASCID) -->
            <xsl:choose>
                <xsl:when test="$measurementData/MASCMeasurementData/ProcessInstanceGroup/ProcessInstance[1]/Data[1][@MSD-MASCID=$MASCID]">
                    <xsl:value-of select="position()"/>
                    <xsl:text>. </xsl:text>
                    <xsl:value-of select="$input//packagedElement[./node[@xmi:id=$baseElement]]/@xmi:type"/>
                    <xsl:text>: </xsl:text>
                    <xsl:value-of select="$input//packagedElement[./node[@xmi:id=$baseElement]]/@name"/>
                    <xsl:value-of select="$newline"/>
                    <xsl:value-of select="$MASCID"/>
                    <xsl:text>, </xsl:text>
                    <xsl:value-of select="$input//node[@xmi:id=$baseElement]/@name"/>
                    <xsl:text>, </xsl:text>
                    <xsl:value-of select="name($input/xmi:XMI/MASC_Profile:*[@AP-MASCID=$MASCID or @GP-MASCID=$MASCID or @MD-MASCID=$MASCID or @MP-MASCID=$MASCID or @UP-MASCID=$MASCID])"/>
                    <xsl:value-of select="$newline"/>
                    <xsl:for-each select="$measurementData/MASCMeasurementData/ProcessInstanceGroup/ProcessInstance[./Data[@MSD-MASCID=$MASCID]]">
                        <xsl:value-of select="./@InstanceUID"/>
                        <xsl:text>, </xsl:text>
                        <xsl:value-of select="normalize-space(./Data/Value/text())"/>
                        <xsl:text>, </xsl:text>
                        <xsl:value-of select="normalize-space(./Data/@TimeStamp)"/>
                        <xsl:value-of select="$newline"/>
                    </xsl:for-each>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="position()"/>
                    <xsl:text>. </xsl:text>
                    <xsl:text>Runtime Measurement Data is not available.</xsl:text>
                    <xsl:value-of select="$newline"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each> 
    </xsl:template>
    
</xsl:stylesheet>
